package parasoft.centrap.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Hashtable;
import java.util.Vector;

public class SuperClassFinder {
	public static int count = 0;
	public static String lastDir = "";
	public static Vector ks = new Vector();
	public static Hashtable ksk = new Hashtable();
	
	public static void main(String[] args) {
	    try {
			showDir(1, new File("/usr/local/CentralReports/webapps/CentralReports/WEB-INF/classes"));
		} catch (IOException e) {
			e.printStackTrace();
		}	
		try {
			save();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
//		Static.printSummary(cr);
		System.out.println("END.");

	}
	
	  static void showDir(int indent, File file) throws IOException {
		  String p = file.getAbsolutePath();
		  if(lastDir.indexOf("classes")!=-1){
			  lastDir = lastDir.replaceAll("classes", "");
		  }
		  if(file.getName().indexOf("$")==-1){

			  if (!file.isDirectory() && p.indexOf("parasoft")!=-1) {
				  p = p.substring(p.indexOf("classes")+8);
				  p = p.replaceAll(".class", "");
				  if(p!=null && p.trim().length()>0){
					  ks.add(p);

				  }
			  }
		    if (file.isDirectory()) {
		    	lastDir += file.getName()+".";
				File[] files = file.listFiles();
				if(files!=null){
					for (int i = 0; i < files.length; i++)
				    	showDir(indent + 4, files[i]);
				}
		    }else{

		    	count++;
		    }
		  }
	  }
	  public static void save() throws ClassNotFoundException{
			try {
				File file = new File("/home/michal/superklasy.txt");
				BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
				for (int i = 0; i < ks.size(); i++) {
					Object o = ks.get(i);
					String status = "";
					if(o!=null){
						if(checkInitClass(o.toString()) && ksk.get(o)!=null){
							status = ksk.get(o).toString();
						}
						if(status!=null && status.trim().length()>0){
							writer.write(ks.get(i).toString().replaceAll("/", ".")+"  "+status);
							writer.newLine();
						}
					}else{
						System.err.println(o+" is NULL??");
					}
				}
				writer.close();
			}
			catch(IOException e) {
				System.err.println("ERROR: " + e);
				return;
			}
		  }
	  public static boolean checkInitClass(final String nazwaKlasy) throws ClassNotFoundException, MalformedURLException{
		  System.out.print("\nChecking "+nazwaKlasy);
		  int last = nazwaKlasy.lastIndexOf("/");
		  String classPath = nazwaKlasy.substring(0,last);
		  File sciezkaKlas = new File("/usr/local/CentralReports/webapps/CentralReports/WEB-INF/classes/"+classPath);
		  Class klasa = null;
		  try{
			  klasa = Class.forName(nazwaKlasy.replaceAll("/", "."),false, new URLClassLoader(new URL []  { sciezkaKlas.toURI().toURL() }));
			  Class superClass = klasa.getSuperclass();
			  if(superClass!=null){
				  ksk.put(nazwaKlasy, superClass.getName());
			  }
		  }catch(ClassNotFoundException cnfe){
		  }catch(NullPointerException e){e.printStackTrace();}
			System.out.print("\t OK!\n");
			return true;
	  }
}
